﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class PhieuBaoHanhDAO
    {
        public List<PhieuBaoHanhDTO> LayDanhSachPhieuBaoHanh()
        {
            List<PhieuBaoHanhDTO> ds = new List<PhieuBaoHanhDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayDanhSachPhieuBaoHanh";
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                PhieuBaoHanhDTO dto = new PhieuBaoHanhDTO();
                dto.MaPhieuBaoHanh = dr.GetInt32(0);
                dto.MaThietBi = dr.GetInt32(1);
                dto.NgayMua = dr.GetDateTime(2);
                dto.NgayHetHan = dr.GetDateTime(3);
                ds.Add(dto);
            }
            dr.Close();
            conn.Close();
            return ds;
        }

        public PhieuBaoHanhDTO LayThongTinPhieuBaoHanh(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "LayThongTinNhanVien";
            cm.Parameters.Add("@MaNhanVien", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            PhieuBaoHanhDTO dto = new PhieuBaoHanhDTO();
            dr.Read();
            dto.MaPhieuBaoHanh = dr.GetInt32(0);
            dto.MaThietBi = dr.GetInt32(1);
            dto.NgayMua = dr.GetDateTime(2);
            dto.NgayHetHan = dr.GetDateTime(3);
            dr.Close();
            conn.Close();
            return dto;
        }

        public void ThemPhieuBaoHanh(PhieuBaoHanhDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_ThemNhanVien";
            cm.Parameters.Add("@MaThietBi", SqlDbType.NVarChar).Value = dto.MaThietBi;
            cm.Parameters.Add("@NgayMua", SqlDbType.NVarChar).Value = dto.NgayMua;
            cm.Parameters.Add("@NgayHetHan", SqlDbType.NVarChar).Value = dto.NgayHetHan;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void XoaPhieuBaoHanh(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_XoaPhieuBaoHanh";
            cm.Parameters.Add("@MaPhieuBaoHanh", SqlDbType.Int).Value = ma;
            cm.ExecuteNonQuery();
            conn.Close();
        }

        public void CapNhatPhieuBaoHanh(PhieuBaoHanhDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_CapNhatPhieuBaoHanh";
            cm.Parameters.Add("@MaPhieuBaoHanh", SqlDbType.Int).Value = dto.MaPhieuBaoHanh;
            cm.Parameters.Add("@MaThietBi", SqlDbType.Int).Value = dto.MaThietBi;
            cm.Parameters.Add("@NgayMua", SqlDbType.DateTime).Value = dto.NgayMua;
            cm.Parameters.Add("@NgayHetHan", SqlDbType.DateTime).Value = dto.NgayHetHan;
            cm.ExecuteNonQuery();
            conn.Close();
        }
    }
}
